// elsePage/pages/pinTuanXianQing/pinTuanXianQing.js
const util = require('../../../utils/util.js')
Page({


  data: {
    imgUrls: getApp().globalData.imgUrls,
    isShare: false,
    activityEnd:true
  },

  onLoad(options) {
    console.log(options, 'options')

    // options = {
    //   fromList: false,
    //   goods_id: '427',
    //   isShare: 'true',
    //   order_id: '342',
    //   pid: '37'
    // }

    if (options.isShare) {
      this.setData({
        order_id: options.order_id,
        isShare: options.isShare,
        pid: options.pid,
        goods_id: options.goods_id,
      })
    }
    this.setData({
      fromList: options.fromList || false,
      order_id: options.order_id
    })
  },

  onShow: function () {
    this.getData()
  },
  goShopDetails() {
    wx.navigateTo({
      url: `/pages/shopDetails/shopDetails?parent_id=${this.data.pid}&goods_id=${this.data.goods_id}&isShare=${this.data.isShare}`,
    })
  },
  getData() {
    console.log(this.data, 'data')
    util.API('pyapi/assemble_if', {
      order_id: this.data.order_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        code,
        order_info,
        msg
      } = res
      if (code == 200) {
        let time = this.intervalTime(order_info.activity[0].etime)
        order_info.assemble_people.map(item => {
          let d = util.getTimeDian(item.create_time)
          item.startTime = `${d.y}-${d.m}-${d.d} ${d.h}:${d.i}:${d.s}`
        })
        this.setData({
          goods_id: order_info.order_goods.goods_id,
          order_info,
          time
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  intervalTime(time) {
    this.setData({
      timer: setInterval(() => {
        var {
          hrStr,
          minStr,
          secStr,
          deviation,
          day,
          shengSR
        } = this.getTimeminus(time)
        deviation--;
        if (deviation < 0) {
          clearInterval(this.data.timer);
          this.setData({
            activityEnd:true,
            shengSR: '00',
            hrStr: '00',
            minStr: '00',
            secStr: '00',
            day: '00',
          })

        } else {
          this.setData({
            activityEnd:false,
            shengSR,
            hrStr,
            minStr,
            secStr,
            deviation,
            day
          });
        }
      }, 1000)
    })
  },
  getTimeminus(current) {
    var deviation, day, hrStr, minStr, secStr, shengSR;
    currentTimeNum = Date.parse(new Date());
    deviation = current - currentTimeNum / 1000;
    //时间差(s)
    day = Math.floor(deviation / 3600 / 24);
    // 换算成天
    hr = Math.floor((deviation) / 3600);
    // 换算成小时
    shengSR = Math.floor(hr % 24);
    // 换算成剩余 小时
    min = Math.floor(((deviation) / 60) % 60);
    // 换算成剩余分
    sec = deviation % 60;
    // 换算成剩余秒

    hrStr = hr.toString();
    minStr = min.toString();
    secStr = sec.toString();
    shengSR = shengSR.toString();
    day = day.toString();
    if (minStr.length == 1) minStr = '0' + minStr;
    if (hrStr.length == 1) hrStr = '0' + hrStr;
    if (secStr.length == 1) secStr = '0' + secStr;
    if (shengSR.length == 1) shengSR = '0' + shengSR;
    if (day.length == 1) day = '0' + day;

    return {
      shengSR,
      hr,
      deviation,
      day,
      hrStr,
      minStr,
      secStr,
    }
  },
guoqi() {
    wx.showToast({
      title: '该团已结束，请选择别的团',
      icon: 'none'
    })
    this.setData({

    })
  },

  goShopDetial() {
    let goods_id = this.data.order_info.order_goods.goods_id,
      activity_id = this.data.order_info.activity_id,
      pid = this.data.pid;
    wx.navigateTo({
      url: `/pages/groupShop/groupShop?isShare=true&pid=${pid}&goods_id=${goods_id}&activity_id=${activity_id}`,
    })
  },
  onShareAppMessage(e) {
    var that = this
    let goods_id = this.data.goods_id,
      order_id = this.data.order_id,
      activity_id = this.data.order_info.activity_id,
      pid = this.data.order_info.pid;
    return {
      title: getApp().globalData.app_name,
      path: `elsePage/pages/pinTuanXianQing/pinTuanXianQing?isShare=${true}&pid=${pid}&goods_id=${goods_id}&order_id=${order_id}`,
      success: () => {
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: () => {
        wx.showToast({
          title: '分享失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },



  onUnload: function () {
    clearInterval(this.data.time)
    if (!this.data.fromList) {
      wx.navigateBack({
        delta: 1
      })
    }
  },


  onReachBottom: function () {

  }
})